<?php
namespace App\Api;

use PhalApi\Api;
use App\redis as Redis;
use App\Domain\Login as Domain_Login;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
include "/www/wwwroot/mangers.flami.live/vendor/phpmailer/phpmailer/src/PHPMailer.php";
include "/www/wwwroot/mangers.flami.live/vendor/phpmailer/phpmailer/src/SMTP.php";
include "/www/wwwroot/mangers.flami.live/vendor/phpmailer/phpmailer/src/Exception.php";
/*include "/www/wwwroot/video.donxfly.com/vendor/phpmailer/phpmailer/src/OAuth.php";
include "/www/wwwroot/video.donxfly.com/vendor/phpmailer/phpmailer/src/OAuthTokenProvider.php";
include "/www/wwwroot/video.donxfly.com/vendor/phpmailer/phpmailer/src/POP3.php";*/
header('Access-Control-Allow-Origin: *'); // guo
header('Content-Type: application/json');
/**
 * 注册登录
 */
 
session_start();
class Login extends Api { 
	public function getRules() {
        return array(
            'getCountrys1'=>array(
            	'field' => array('name' => 'field', 'type' => 'string', 'default'=>'', 'desc' => '搜索json串'),
            ),
			'getCountrys'=>array(
            	'field' => array('name' => 'field', 'type' => 'string', 'default'=>'', 'desc' => '搜索json串'),
            	'recommend' => array('name' => 'recommend', 'type' => 'int', 'default'=>'2', 'desc' => '1不推荐2推荐'),//
            ),
            
            'getLanguages1'=>array(
            	'field' => array('name' => 'field', 'type' => 'string', 'default'=>'', 'desc' => '搜索json串'),
            ),
            
            'getLanguages'=>array(
            	'field' => array('name' => 'field', 'type' => 'string', 'default'=>'', 'desc' => '搜索json串'),
            	'recommend' => array('name' => 'recommend', 'type' => 'int', 'default'=>'2', 'desc' => '1不推荐2推荐'),//
            ),
            
            'userRegister' => array(
                'gmail' => array('name' => 'gmail', 'type' => 'string', 'desc' => '用户注册邮箱'),
                'code' => array('name' => 'code', 'type' => 'string', 'desc' => '验证码'),
                'source' => array('name' => 'source', 'type' => 'int', 'default'=>'0', 'desc' => '来源设备,0web，1android，2ios，3小程序'),
            ),
            'userRegisterByDevice' => array(
                'device' => array('name' => 'device', 'type' => 'string', 'desc' => '游客登陆'),
                'source' => array('name' => 'source', 'type' => 'int', 'default'=>'0', 'desc' => '来源设备,0web，1android，2ios，3小程序'),
            ),
            
            'getGmailCode' => array(
				'gmail' => array('name' => 'gmail', 'type' => 'string', 'desc' => '邮箱'),
				'source' => array('name' => 'source', 'type' => 'int', 'default'=>'0', 'desc' => ''),//
			),

		    
			'userLogin' => array(
                'user_login' => array('name' => 'user_login', 'type' => 'string', 'desc' => '账号'),
				'country_code' => array('name' => 'country_code', 'type' => 'string', 'desc' => '账号-区号'),
				'code' => array('name' => 'code', 'type' => 'string', 'desc' => '验证码'),
				'source' => array('name' => 'source', 'type' => 'int', 'default'=>'0', 'desc' => '来源设备,0web，1android，2ios，3小程序'),
            ),

			'userLoginByThird' => array(
                'openid' => array('name' => 'openid', 'type' => 'string', 'desc' => '第三方openid'),
                'type' => array('name' => 'type', 'type' => 'int', 'default'=>'0', 'desc' => '第三方标识,0PC，1QQ，2微信，3新浪，4facebook，5twitter'),
                'nicename' => array('name' => 'nicename', 'type' => 'string',   'default'=>'',  'desc' => '第三方昵称'),
                'avatar' => array('name' => 'avatar', 'type' => 'string',  'default'=>'', 'desc' => '第三方头像'),
                'sign' => array('name' => 'sign', 'type' => 'string',  'default'=>'', 'desc' => '签名'),
                'source' => array('name' => 'source', 'type' => 'int',  'default'=>'0', 'desc' => '来源设备,0web，1android，2ios，3小程序'),
                'access_token' => array('name' => 'access_token', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '三方接口调用凭证'),
            ),
			
			'getCode' => array(
				'mobile' => array('name' => 'mobile', 'type' => 'string', 'desc' => '手机号'),
				'country_code' => array('name' => 'country_code', 'type' => 'string', 'desc' => '手机号-区号'),
                'sign' => array('name' => 'sign', 'type' => 'string',  'default'=>'', 'desc' => '签名'),
			),

            'logout' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户Token'),
			),
			
			'getCancelCondition'=>array(
            	'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户Token'),
            ),
			
			'cancelAccount'=>array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户Token'),
                'time' => array('name' => 'time', 'type' => 'string', 'desc' => '时间戳'),
                'sign' => array('name' => 'sign', 'type' => 'string', 'desc' => '签名'),
            ),
			
			'setUserOnline' => array(
			    'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户Token'),
				'online' => array('name' => 'online', 'type' => 'int','default'=>'0', 'desc' => '状态，0离线，1勿扰，2在聊，3在线'),
            ),
        );
	}
	
	
    public function send_email($code,$email){
        $mail = new PHPMailer(true);

        try {
            // 服务器配置
            $mail->isSMTP();
            $mail->Host = 'smtp.gmail.com';
            $mail->SMTPAuth = true;
            $mail->Username = 'flamigo.chat@gmail.com'; // 替换为你的Gmail
            $mail->Password = 'devdynsvuirqecsc'; // 使用应用专用密码
            $mail->SMTPSecure = 'tls';
            $mail->Port = 587;
        
            // 收件人设置
            $mail->setFrom('flamigo.chat@gmail.com', 'FLAMI');
            $mail->addAddress($email, '');
            
            // 邮件内容
            $mail->isHTML(true);
            $mail->Subject = 'Email verification';
            $mail->Body = '<h1>'.$code.'</h1>';
            //$mail->AltBody = '纯文本内容（当邮件客户端不支持HTML时显示）';
            $mail->AltBody = $code;
        
            return $mail->send();
        } catch (Exception $e) {
            echo "发送失败: {$mail->ErrorInfo}";
        }
    }
    
    /**
     * 登录方式开关信息
     * @desc 用于获取登录方式开关信息
     * @return int code 操作码，0表示成功
     * @return array info 
     * @return string info[][0] 登录方式标识

     * @return string msg 提示信息
     */
    public function getLoginType() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $configpri=\App\getConfigPri();
		
		$info = \App\getConfigPub();
		//登录弹框那个地方
        $login_alert=array(
            'title'=>$info['login_alert_title'],
            'content'=>$info['login_alert_content'],
            'login_title'=>$info['login_clause_title'],
            'message'=>array(
                array(
                    'title'=>$info['login_service_title'],
                    'url'=>\App\get_upload_path($info['login_service_url']),
                ),
                array(
                    'title'=>$info['login_private_title'],
                    'url'=>\App\get_upload_path($info['login_private_url']),
                ),
            )
        );
		
		
		$login_type=$configpri['login_type'];
        foreach ($login_type as $k => $v) {
            if($v=='ios'){
                unset($login_type[$k]);
                break;
            }
        }

        $login_type=array_values($login_type);

        $rs['info'][0]['login_alert'] = $login_alert;
        $rs['info'][0]['login_type'] = $login_type;
        $rs['info'][0]['login_type_ios'] = $configpri['login_type'];



        return $rs;
    }
	
	public function getCountrys() {
		$rs = array('code' => 0, 'msg' => '', 'info' => array());
		
		$field=\App\checkNull($this->field);
		$recommend=\App\checkNull($this->recommend);
		$where = 'type=0';
		if(empty($recommend)){
		}else{
		    $where.=' and recommend='.$recommend;
		}
		if($field!=''){
            $where.=" and (name like '%".$field."%' or name_str like '%".$field."%')";
        }
        
		$info = \PhalApi\DI()->notorm->country
					->select('*')
					->where($where)
                    ->order('title asc')
					->fetchAll();
		$arr = [['name'=>'全部','name_str'=>'{"en":"All"}','code'=>'']];
		$info = array_merge($arr,$info);
        if(!empty($info)){
            foreach ($info as &$v){
                $v['name_str'] = json_decode($v['name_str'],true);
                if(!empty($v['img'])){
                    $v['img'] = \App\get_upload_path($v['img']);
                }
            }
        }
	    if(empty($recommend) && $field==''){
	        $titles = array_values(array_unique(array_column($info,'title')));
	        $newdata = [];
	        foreach ($titles as $k=>$v){
	            $newdata[$k]['title'] = $v;
	            $newdata[$k]['lists'] = \PhalApi\DI()->notorm->country
					->select('*')
					->where('title',$v)
					->where($where)
                    ->order('id asc')
					->fetchAll();
				foreach ($newdata[$k]['lists'] as &$v1){
				    $v1['name_str'] = json_decode($v1['name_str'],true);
				    $v1['img'] = \App\get_upload_path($v1['img']);
				}
	        }
            $info = $newdata;
	    }
		$rs['info']=$info;
		return $rs;
	}
	
	public function getLanguages() {
		$rs = array('code' => 0, 'msg' => '', 'info' => array());
		
		$field=\App\checkNull($this->field);
		$recommend=\App\checkNull($this->recommend);
		$where = 'type=1';
		if(empty($recommend)){
		}else{
		    $where.=' and recommend='.$recommend;
		}
		if($field!=''){
            $where.=" and (name like '%".$field."%' or name_str like '%".$field."%')";
        }
		$info = \PhalApi\DI()->notorm->country
					->select('*')
					->where($where)
                    ->order('title ASC')
					->fetchAll();
		$arr = [['name'=>'全部','name_str'=>'{"en":"All"}','code'=>'']];
		$info = array_merge($arr,$info);
		if(!empty($info)){
            foreach ($info as $k=>$v){
                $info[$k]['name_str'] = json_decode($v['name_str'],true);
            }
        }
	    if(empty($recommend) && $field==''){
	        $titles = array_values(array_unique(array_column($info,'title')));
	        $newdata = [];
	        foreach ($titles as $k=>$v){
	            $newdata[$k]['title'] = $v;
	            $newdata[$k]['lists'] = \PhalApi\DI()->notorm->country
					->select('*')
					->where($where)
					->where('title',$v)
                    ->order('title asc')
					->fetchAll();
				foreach ($newdata[$k]['lists'] as $k1=>$v1){
				    $newdata[$k]['lists'][$k1]['name_str'] = json_decode($v1['name_str'],true);
				}
	        }
            $info = $newdata;
	    }
		$rs['info']=$info;
		return $rs;
	}
	
// 	public function insertCountrys() {
		
// 		//$field=\App\checkNull($this->field);
		
// 		$key='getCountrys';
	
// 		$city=API_ROOT.'/config/country.json';//文件存放目录
// 		// 从文件中读取数据到PHP变量 
// 		$json_string = file_get_contents($city); 
// 		 // 用参数true把JSON字符串强制转成PHP数组 
// 		$data = json_decode($json_string, true);

// 		$info=$data['country']; //国家
// 		if(!empty($info)){
// 		    $data1 = [];
		   
// 		    foreach ($info as $k=>$v){
// 		        if(!empty($v['lists'])){
// 		            foreach ($v['lists'] as $k1=>$v1){
// 		                $data1['name'] = $v1['name'];
// 		                $data1['name_str'] = json_encode(['en'=>$v1['name_en']]);
// 		                $data1['code'] = $v1['code'];
// 		                $data1['create_time'] = date('Y-m-d H:i:s');
// 		                $data1['title'] = $v['title'];
// 		                \PhalApi\DI()->notorm->country->insert($data1);
		                
// 		            }
// 		        }
		        
// 		    }
// 		}

// 	}
	
// 	public function insertLanguage() {
		
// 		//$field=\App\checkNull($this->field);
		
// 		$key='getCountrys';
	
// 		$city=API_ROOT.'/config/language.json';//文件存放目录
// 		// 从文件中读取数据到PHP变量 
// 		$json_string = file_get_contents($city); 
// 		 // 用参数true把JSON字符串强制转成PHP数组 
// 		$data = json_decode($json_string, true);
//         //print_r($data);exit;
// 		$info=$data['languages']; //语言
// 		if(!empty($info)){
// 		    $data1 = [];
		   
// 		    foreach ($info as $k=>$v){
// 		        if(!empty($v['lists'])){
// 		            foreach ($v['lists'] as $k1=>$v1){
// 		                $data1['name'] = $v1['name'];
// 		                $data1['name_str'] = json_encode(['en'=>$v1['name_en']]);
// 		                $data1['code'] = $v1['code'];
// 		                $data1['create_time'] = date('Y-m-d H:i:s');
// 		                $data1['title'] = $v['title'];
// 		                $data1['type'] = 1;
// 		                \PhalApi\DI()->notorm->country->insert($data1);
		                
// 		            }
// 		        }
		        
// 		    }
// 		}

// 	}
	
	/*public function getLanguages() {
		$rs = array('code' => 0, 'msg' => '', 'info' => array());
		
		$field=\App\checkNull($this->field);
		
		$key='getLanguages';
		$info=\App\getcaches($key);
		if(!$info){
			$language=API_ROOT.'/config/language.json';//文件存放目录
			// 从文件中读取数据到PHP变量 
			$json_string = file_get_contents($language); 
			 // 用参数true把JSON字符串强制转成PHP数组 
			$data = json_decode($json_string, true);

			$info=$data['languages']; //国家
			
			\App\setcaches($key,$info);
		}
		
		
		if($field){
			$rs['info']=$this->array_searchs($field,$info);
			return $rs;
		}
	 
		$rs['info']=$info;
		return $rs;
	}*/
	
	
	
	private function array_searchs($field,$data) {
	    $arr=array();
	    foreach($data as $k => $v){
        
            $lists=$v['lists'];
        
            foreach ($lists as $k => $v) {
                
                if(strstr($v['name'], $field) !== false){
                    array_push($arr, $v);
                }
            }
        }
	    
		return $arr;
	}
	
	
	
	
    /**
     * 会员登陆
     * @desc 用于用户登陆信息
     * @return int code 操作码，0表示成功
     * @return array info 用户信息
     * @return string info[0].id 用户ID
     * @return string info[0].user_nickname 昵称
     * @return string info[0].avatar 头像
     * @return string info[0].avatar_thumb 头像缩略图
     * @return string info[0].sex 性别
     * @return string info[0].signature 签名
     * @return string info[0].coin 用户余额
     * @return string info[0].login_type 注册类型
     * @return string info[0].level 等级
     * @return string info[0].level_anchor 主播等级
     * @return string info[0].birthday 生日
     * @return string info[0].token 用户Token
     * @return string info[0].isauth 是否用户认证，0否1是
     * @return string info[0].isauthor_auth 是否主播认证，0否1是
     * @return string info[0].usersig IM签名
     * @return string msg 提示信息
     */
    public function userLogin() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
		$user_login=\App\checkNull($this->user_login);
		$code=\App\checkNull($this->code);
		$source=\App\checkNull($this->source);
		$country_code=\App\checkNull($this->country_code);

        if($user_login==''){
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('请输入手机号');
            return $rs;	
        }
        
        if($code==''){
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('请输入验证码');
            return $rs;	
        }
        
        if(!isset($_SESSION['reg_mobile']) || !$_SESSION['reg_mobile'] || !isset($_SESSION['reg_mobile_code']) || !$_SESSION['reg_mobile_code'] ){
            $rs['code'] = 1003;
            $rs['msg'] = \PhalApi\T('请先获取验证码');
            return $rs;		
        }
		
		if($user_login!=$_SESSION['reg_mobile']){
            $rs['code'] = 1004;
            $rs['msg'] = \PhalApi\T('手机号码不一致');
            return $rs;					
		}
        
        if($country_code!=$_SESSION['reg_country_code']){
            $rs['code'] = 1004;
            $rs['msg'] = \PhalApi\T('国家区号不一致');
            return $rs;					
		}

		if($code!=$_SESSION['reg_mobile_code']){
            $rs['code'] = 1005;
            $rs['msg'] = \PhalApi\T('验证码错误');
            return $rs;					
		}
        
        $user_pass='live1v1'.time();
        
        $domain = new Domain_Login();
        $info = $domain->userLogin($user_login,$user_pass,$source,$country_code);

		if($info==1001){
			$rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('账号或密码错误');
            return $rs;	
		}else if($info==1002){
			$rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('该账号已被禁用');
            return $rs;	
		}else if($info==1003){
			$rs['code'] = 1003;
            $rs['msg'] = \PhalApi\T('该账号已被注销');
            return $rs;	
		}
	
        $rs['info'][0] = $info;
        
        unset($_SESSION['reg_mobile']);
        unset($_SESSION['reg_mobile_code']);
        unset($_SESSION['reg_mobile_expiretime']);
		
        return $rs;
    }	
    
    
    public function userRegister() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
		$gmail=\App\checkNull($this->gmail);
		$code=\App\checkNull($this->code);
		$source=\App\checkNull($this->source);
		//$country_code=\App\checkNull($this->country_code);

        if($gmail==''){
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('请输入邮箱');
            return $rs;	
        }
        
        if($code==''){
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('请输入验证码');
            return $rs;	
        }
        
        if(!empty($source)){
//            if(!isset($_SESSION['reg_gmail']) || !$_SESSION['reg_gmail'] || !isset($_SESSION['reg_gmail_code']) || !$_SESSION['reg_gmail_code'] ){
//                $rs['code'] = 1003;
//                $rs['msg'] = \PhalApi\T('请先获取验证码');
//                return $rs;
//            }
//
//    		if($gmail!=$_SESSION['reg_gmail']){
//                $rs['code'] = 1004;
//                $rs['msg'] = \PhalApi\T('邮箱不一致');
//                return $rs;
//    		}
            
    //         if($country_code!=$_SESSION['reg_country_code']){
    //             $rs['code'] = 1004;
    //             $rs['msg'] = \PhalApi\T('国家区号不一致');
    //             return $rs;					
    // 		}
    
//    		if($code!=$_SESSION['reg_gmail_code']){
//                $rs['code'] = 1005;
//                $rs['msg'] = \PhalApi\T('验证码错误');
//                return $rs;
//    		}
        }else{
            
            $r_gmail = \App\getcache($gmail);
		    $r_code = \App\getcaches($code);
//		    if(empty($r_gmail) || empty($r_code)){
//		        $rs['code'] = 1003;
//                $rs['msg'] = \PhalApi\T('请先获取验证码');
//                return $rs;
//		    }
//		    if($r_code != $code){
//		        $rs['code'] = 1005;
//                $rs['msg'] = \PhalApi\T('验证码错误');
//                return $rs;
//		    }
//
//		    if($r_gmail != $gmail){
//		        $rs['code'] = 1004;
//                $rs['msg'] = \PhalApi\T('邮箱不一致');
//                return $rs;
//		    }
        }
        
        
        $user_pass='live1v1'.time();
        
        $domain = new Domain_Login();
        $country_code = "1";
        $info = $domain->userLogin($gmail,$user_pass,$source,$country_code);

		if($info==1001){
			$rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('账号或密码错误');
            return $rs;	
		}else if($info==1002){
			$rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('该账号已被禁用');
            return $rs;	
		}else if($info==1003){
			$rs['code'] = 1003;
            $rs['msg'] = \PhalApi\T('该账号已被注销');
            return $rs;	
		}
	
        $rs['info'][0] = $info;
        
        unset($_SESSION['reg_mobile']);
        unset($_SESSION['reg_mobile_code']);
        unset($_SESSION['reg_mobile_expiretime']);
		
        return $rs;
    }
    
    public function userRegisterByDevice() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
		$device=\App\checkNull($this->device);
		$source=\App\checkNull($this->source);
		//$country_code=\App\checkNull($this->country_code);

        if($device==''){
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('参数错误1');
            return $rs;	
        }
        
        $user_pass='live1v1'.time();
        
        $domain = new Domain_Login();
        $country_code = "1";
        $info = $domain->userLoginByDevice($device,$user_pass,$source,$country_code);

		if($info==1001){
			$rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('参数错误2');
            return $rs;	
		}else if($info==1002){
			$rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('该账号已被禁用');
            return $rs;	
		}else if($info==1003){
			$rs['code'] = 1003;
            $rs['msg'] = \PhalApi\T('该账号已被注销');
            return $rs;	
		}
	
        $rs['info'][0] = $info;
		
        return $rs;
    }	


    /**
     * 第三方登录
     * @desc 用于用户登陆信息
     * @return int code 操作码，0表示成功
     * @return array info 用户信息
     * @return string info[0].id 用户ID
     * @return string info[0].user_nickname 昵称
     * @return string info[0].avatar 头像
     * @return string info[0].avatar_thumb 头像缩略图
     * @return string info[0].sex 性别
     * @return string info[0].signature 签名
     * @return string info[0].coin 用户余额
     * @return string info[0].login_type 注册类型
     * @return string info[0].level 等级
     * @return string info[0].level_anchor 主播等级
     * @return string info[0].birthday 生日
     * @return string info[0].token 用户Token
     * @return string info[0].isauth 是否用户认证，0否1是
     * @return string info[0].isauthor_auth 是否主播认证，0否1是
     * @return string info[0].usersig IM签名
     * @return string msg 提示信息
     */
    public function userLoginByThird() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
		$openid=\App\checkNull($this->openid);
		$type=\App\checkNull($this->type);
		$nicename=\App\checkNull($this->nicename);
		$avatar=\App\checkNull($this->avatar);
		$source=\App\checkNull($this->source);
		$sign=\App\checkNull($this->sign);
		$access_token=\App\checkNull($this->access_token);
        
        if($openid=='' || $type=='' || $sign==''){
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('信息错误');
            return $rs;	
        }
        
        $checkdata=array(
            'openid'=>$openid
        );
        
        $issign=\App\checkSign($checkdata,$sign);
        if(!$issign){
            $rs['code']=1001;
			$rs['msg']=\PhalApi\T('签名错误');
			return $rs;	
        }

        if($type == 'wx'){
            $url="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid;

            $res=$this->checkThirdUserInfo($url,$type);

            if($res['code'] !=0){
                $res['msg']=\PhalApi\T('信息验证失败');
                return $res;
            }

            
        }else if($type == 'qq'){

            $configpri=\App\getConfigPri();
            $url="https://graph.qq.com/user/get_user_info?access_token=".$access_token."&oauth_consumer_key=".$configpri['login_qq_appid']."&openid=".$openid;

            $res=$this->checkThirdUserInfo($url,$type);
            
            if($res['code'] !=0){
                $res['msg']=\PhalApi\T('信息验证失败');
                return $res;
            }

        }

        $domain = new Domain_Login();
        $info = $domain->userLoginByThird($openid,$type,$nicename,$avatar,$source);
		
        if($info==1001){
			$rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('账号或密码错误');
            return $rs;	
		}else if($info==1002){
			$rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('该账号已被禁用');
            return $rs;	
		}else if($info==1003){
			$rs['code'] = 1003;
            $rs['msg'] = \PhalApi\T('该账号已被注销');
            return $rs;	
		}
		
		//判断是否被鉴黄禁用账号
		$isJianhuangBlack=\App\checkIsJianhuangBlack($info['id'],'0');
		if($isJianhuangBlack['code']!=0){
			return $isJianhuangBlack;	
		}

        $rs['info'][0] = $info;

        return $rs;
    }
	
	/**
	 * 获取注册短信验证码
	 * @desc 用于注册获取短信验证码
	 * @return int code 操作码，0表示成功,2发送失败
	 * @return array info 
	 * @return string msg 提示信息
	 */
	 
	public function getCode() {
		$rs = array('code' => 0, 'msg' => \PhalApi\T('发送成功，请注意查收'), 'info' => array());
		
		$mobile = \App\checkNull($this->mobile);
		$country_code = \App\checkNull($this->country_code);
		
		$sign = \App\checkNull($this->sign);
		
        if($mobile==''){
			$rs['code']=1001;
			$rs['msg']=\PhalApi\T('请输入手机号');
			return $rs;
		}
        
		$ismobile=\App\checkMobile($mobile,$country_code);
		
		if($ismobile==1001){
			$rs['code']=1001;
			$rs['msg']=\PhalApi\T('国际/港澳台不支持国内区号');
			return $rs;
		}else if($ismobile==1002){
			$rs['code']=1002;
			$rs['msg']=\PhalApi\T('国内不支持国际/港澳台区号');
			return $rs;	
		}else if(!$ismobile){
			$rs['code']=1001;
			$rs['msg']=\PhalApi\T('请输入正确的手机号');
			return $rs;	
		}
        
        $checkdata=array(
            'mobile'=>$mobile
        );
        
        $issign=\App\checkSign($checkdata,$sign);
        if(!$issign){
            $rs['code']=1001;
			$rs['msg']=\PhalApi\T('签名错误');
			return $rs;
        }
		
		$iscancellation=\App\checkIsDestroyByLogin($mobile,'1');
		if($iscancellation){
			$rs['code']=1001;
			$rs['msg']=\PhalApi\T('该账号已被注销,不能使用');
			return $rs;	
		}
		
		
		//判断是否被鉴黄禁用账号
		$isJianhuangBlack=\App\checkIsJianhuangBlack($mobile,'1');
		if($isJianhuangBlack['code']!=0){
			return $isJianhuangBlack;	
		}
		

		if(isset($_SESSION['reg_mobile']) && $_SESSION['reg_mobile']==$mobile && $_SESSION['reg_mobile_expiretime']> time() ){
			$rs['code']=1002;
			$rs['msg']=\PhalApi\T('验证码5分钟有效，请勿多次发送');
			return $rs;
		}
		
        $limit = \App\ip_limit();
		if( $limit == 1){
			$rs['code']=1003;
			$rs['msg']=\PhalApi\T('您已当日发送次数过多');
			return $rs;
		}
		$mobile_code = \App\random(6,1);
		
		/* 发送验证码 */
 		$result=\App\sendCode($mobile,$mobile_code,$country_code);
		if($result['code']==0){
			$_SESSION['reg_mobile'] = $mobile;
			$_SESSION['reg_mobile_code'] = $mobile_code;
			$_SESSION['reg_country_code'] = $country_code;
			$_SESSION['reg_mobile_expiretime'] = time() +60*5;	
		}else if($result['code']==667){
			$_SESSION['reg_mobile'] = $mobile;
            $_SESSION['reg_mobile_code'] = $result['msg'];
			$_SESSION['reg_country_code'] = $country_code;
            $_SESSION['reg_mobile_expiretime'] = time() +60*5;
            
            $rs['code']=1002;
			$rs['msg']=\PhalApi\T('验证码为：').$result['msg'];
		}else{
			$rs['code']=1002;
			$rs['msg']=$result['msg'];
		} 
		
		
		return $rs;
	}
	
	public function getGmailCode() {
		$rs = array('code' => 0, 'msg' => \PhalApi\T('发送成功，请注意查收'), 'info' => array());
		
		$gmail = \App\checkNull($this->gmail);

		
        if($gmail==''){
			$rs['code']=1001;
			$rs['msg']=\PhalApi\T('请输入邮箱');
			return $rs;
		}


		//$gmail_code = \App\random(6,1);
		$gmail_code = '123456';
		//$result = $this->send_email($gmail_code,$gmail);
		$result =1;
		if($result != 1){
		    $rs['code']=1002;
			$rs['msg']=\PhalApi\T('发送失败');
			return $rs;
		}
		if($this->source == 1){
		    \App\setcaches($gmail,$gmail,300);
		    \App\setcaches($gmail_code,$gmail_code,300);
		}else{
		    /* 发送验证码 */
     		$_SESSION['reg_gmail'] = $gmail;
    		$_SESSION['reg_gmail_code'] = $gmail_code;
    		$_SESSION['reg_gmail_expiretime'] = time() +60*5;
		}
		
		return $rs;
	}	


	/**
	 * 退出
	 * @desc 用于用户退出 注销极光
	 * @return int code 操作码，0表示成功
	 * @return array info 
	 * @return string msg 提示信息
	 */
	/* public function logout() {
		$rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        $uid = $this->uid;
		$token=\App\checkNull($this->token);
        
        if($uid=='' || $token=='' ){
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('信息错误');
            return $rs;	
        }
        
		$checkToken=\App\checkToken($uid,$token);
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}

		$info = \App\userLogout($uid);
		return $rs;			
	} */
	
	
	/**
     * 获取注销账号的条件
     * @desc 用于获取注销账号的条件
     * @return int code 状态码，0表示成功
     * @return string msg 提示信息
     * @return array info 返回信息
     * @return array info[0]['list'] 条件数组
     * @return string info[0]['list'][]['title'] 标题
     * @return string info[0]['list'][]['content'] 内容
     * @return string info[0]['list'][]['is_ok'] 是否满足条件 0 否 1 是
     * @return string info[0]['can_cancel'] 是否可以注销账号 0 否 1 是
     */
    public function getCancelCondition(){
    	$rs = array('code' => 0, 'msg' => '', 'info' => array());
        $uid=\App\checkNull($this->uid);
        $token=\App\checkNull($this->token);
        $checkToken=\App\checkToken($uid,$token);
        if($checkToken==700){
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        $domain=new Domain_Login();
        $res=$domain->getCancelCondition($uid);

        $rs['info'][0]=$res;

        return $rs;
    }

    /**
     * 用户注销账号
     * @desc 用于用户注销账号
     * @return int code 状态码,0表示成功
     * @return string msg 返回提示信息
     * @return array info 返回信息
     */
    public function cancelAccount(){
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $uid=\App\checkNull($this->uid);
        $token=\App\checkNull($this->token);
        $time=\App\checkNull($this->time);
        $sign=\App\checkNull($this->sign);

        $checkToken=\App\checkToken($uid,$token);
        if($checkToken==700){
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        if(!$time||!$sign){
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('参数错误');
            return $rs;
        }

        $now=time();
        if($now-$time>300){
            $rs['code']=1001;
            $rs['msg']=\PhalApi\T('参数错误');
            return $rs;
        }

        
        $checkdata=array(
            'uid'=>$uid,
            'token'=>$token,
            'time'=>$time
        );
        
        $issign=\App\checkSign($checkdata,$sign);
        if(!$issign){
            $rs['code']=1001;
            $rs['msg']=\PhalApi\T('签名错误');
            return $rs; 
        }

        $domain=new Domain_Login();
        $res=$domain->cancelAccount($uid);

        if($res==1001){
        	$rs['code']=1001;
            $rs['msg']=\PhalApi\T('相关内容不符合注销账号条件');
            return $rs;
        }

        $rs['msg']=\PhalApi\T('注销成功,手机号、身份证号等信息已解除');
        return $rs;
    }
	
	
	/**
     * 用户状态
     * @desc 用于更新用户在线状态
     * @return int code 状态码,0表示成功
     * @return string msg 返回提示信息
     * @return array info 返回信息
     */
    public function setUserOnline(){
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $uid=\App\checkNull($this->uid);
        $token=\App\checkNull($this->token);
        $online=$this->online;
 

        $checkToken=\App\checkToken($uid,$token);
        if($checkToken==700){
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        if($online==''){
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('参数错误');
            return $rs;
        }
		
		$domain=new Domain_Login();
        $domain->setUserOnline($uid,$online);
		
        return $rs;
    }


    /**
     * 验证三方登录用户信息
     * @param  string $url api地址
     * @param  string $type api类型
     * @return int code 状态码 0表示验证成功
     * @return string msg 返回提示信息
     * @return  code 状态码 0表示验证成功
     * 微信:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Authorized_API_call_UnionID.html
     * qq:https://wiki.connect.qq.com/%e5%bc%80%e5%8f%91%e6%94%bb%e7%95%a5_server-side
     */
    private function checkThirdUserInfo($url,$type){

        $rs=array('code'=>0,'msg'=>'','info'=>array());

        $header = array(
           'Accept: application/json',
        );
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

        $result = curl_exec($curl);
     

        if (curl_error($curl)) {
            $rs['code']=1001;
            $rs['msg']=\PhalApi\T('信息验证失败');
            
        } else {

            // 打印返回的内容
            $res_arr=json_decode($result,true);

            if($type=='wx'){

                //验证失败
                if(isset($res_arr['errcode'])){
                    $rs['code']=$res_arr['errcode'];
                    $rs['msg']=$res_arr['errmsg'];
                }

            }else if($type=='qq'){
                //验证失败
                if($res_arr['ret'] !=0){
                    $rs['code']=50001;
                    $rs['msg']=$res_arr['msg'];
                }
            }
            
            curl_close($curl);

        }

        return $rs;

    }

}
